import Vue from 'vue'
import VueRouter from 'vue-router'
import UserLogin from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/welcome.vue'
import User from '../components/User/User.vue'
import Category from '../components/Product/category.vue'
import Setmeal from '../components/Product/SetmealList.vue'
import Product from '../components/Product/ProductList.vue'
import Report from '../components/Report/report.vue'
import workSpace from '../components/WorkSpace/workSpace.vue'
import productPurchase from '../components/UserShop/userPurchase.vue';
import PaymentDialog from '../components/UserShop/PaymentDialog.vue'
import Address from '../components/UserAddress/AddressBook.vue'
import Signup from '../components/Signup.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/', 
    redirect: '/login'
  },
  {
    path: '/login',
    component: UserLogin
  },
  {
    path: '/signup',
    component: Signup
  },
  {
    path: '/payment',
    name: 'payment',
    component: PaymentDialog
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {
        path: '/welcome' ,
        component: Welcome
      },
      {
        path:'/users',
        component: User
      },
      {
        path:'/category',
        component: Category
      },{
        path:'/setmeals',
        component: Setmeal
      },{
        path:"/products",
        component: Product
      },
      {
        path:"/report",
        component: Report
      },
      {
        path:"/workspace",
        component: workSpace
      },
      {
        path: '/address',
        component:Address,
      }
    ]
  },
  {
    name:'buy',
    path: '/buy',
    component: productPurchase, // 新页面显示购买产品页面
  },
 
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
    if(to.path === '/login' || to.path === '/signup') {
      return next();
    }
    else{
      const token = window.sessionStorage.getItem("token");
      if(!token){
        return next('/login')
      }
      else{
        next()
      }
    }
})
export default router
